//创建服务器
const express = require('express')
const app = express()
app.listen(54188)

// 导入路由模块
const frontRouter = require('./router/front/index.js')
const adminRouter = require('./router/admin/index.js')

//导入记录日志的中间件
const writeLog = require('./middleware/writeLog')
//导入写入错误日志的工具库
const writeErr = require('./utils/writeErr.js')

//导入响应函数
const respJson = require('./utils/respJson.js')

//导入body-parser
const bodyParser = require('body-parser')

//日志记录中间件 - 应用级别全局中间件
app.use(writeLog) //会经过后面的中间件全部都要经过这个

//获取post数据中间件
app.use(bodyParser.urlencoded({
    extended: false
}))

//调试报错
// app.use('/',(req, resp)=>{
//     let a
//     console.log(a.hah);
// })

//前台操作,用户相关
app.use('/front', frontRouter)
//后台操作,管理员
app.use('/admin', adminRouter)


//异常处理中间件
app.use((err, req, resp, next) => {
    writeErr(err)
    respJson(resp, 500, '系统繁忙，稍后再试！')
})

//404中间件
app.use((req, resp, next) => {
    writeErr('404')
    respJson(resp, 404, '请求路径错误！')
})